import JSZip from 'jszip'

export type FileType = 'application/pdf'

/**
 * @description blob转arrayBuffer
 * @param blob
 */
export function getBufferWithBlob(blob: Blob): Promise<ArrayBuffer> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = function (event) {
      // const data = new Uint8Array(event.target?.result as ArrayBuffer)
      // resolve(data)
      const arrayBuffer = event.target?.result as ArrayBuffer
      resolve(arrayBuffer)
    }
    reader.onerror = function (error) {
      reject(error)
    }
    reader.readAsArrayBuffer(blob)
  })
}

/**
 * @description arrayBuffer转blob
 * @param data
 * @param type
 * @returns
 */
export const getBlobWithBuffer = (data: Uint8Array, type: FileType): Blob => {
  return new Blob([data], { type: type })
}

/**
 * @description blob转base64
 * @param blob
 */
export const getBase64WithBlob = (blob: Blob): Promise<string> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(blob)
    reader.onload = (e) => {
      resolve(e.target!.result as string)
    }
    reader.onerror = (e) => {
      reject(e)
    }
  })
}

/**
 * @description base64转blob
 * @param base64
 * @returns
 */
export const getBlobWithBase64 = (base64: string): Blob => {
  let arr = base64.split(',')
  let mime = arr[0].match(/:(.*?);/)?.[1] ?? 'application/octet-stream' // 设置默认 MIME 类型
  let bstr = atob(arr[1])
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

/**
 * @description blob转url
 * @param blob
 * @returns
 */
export const getUrlWithBlob = (blob: Blob): string => URL.createObjectURL(blob)

/**
 * @description 下载文件
 * @param url
 * @param filename
 */
export const downloadFile = (url: string, filename: string) => {
  let element = document.createElement('a')
  element.style.display = 'none'
  element.setAttribute('href', url)
  element.setAttribute('download', filename)
  document.body.appendChild(element)
  element.click()
  document.body.removeChild(element)
}

/**
 * @description 下载图片zip
 * @param imageBlobs
 * @param fileName
 * @param fileType
 */
export const downloadImages = (base64List: string[], fileName: string, fileType: string) => {
  // 创建一个新的JSZip实例
  const zip = new JSZip()
  // 创建一个名为filename的文件夹
  const imgFolder = zip.folder(fileName)

  // base64转blob
  Promise.all(base64List.map((item) => getBlobWithBase64(item))).then((imageBlobs) => {
    // 给图片文件命名
    imageBlobs.forEach((blob, index) => {
      imgFolder!.file(`${fileName}_${index + 1}${fileType}`, blob)
    })

    // 生成ZIP文件内容
    zip.generateAsync({ type: 'blob' }).then((content: Blob) => {
      const link = document.createElement('a')
      link.href = URL.createObjectURL(content)
      link.download = `${fileName}.zip`
      link.click()
      URL.revokeObjectURL(link.href)
    })
  })
}

/**
 * @description 获取canvas
 * @param width
 * @param height
 * @returns
 */
export const getCanvasContext = (width: number, height: number) => {
  const canvas = document.createElement('canvas')
  const context: CanvasRenderingContext2D = canvas.getContext('2d')!
  canvas.width = width
  canvas.height = height
  return { canvas, context }
}
